#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct person {
    char* str;
} PERSON;

int sortStart(char* s1, char* s2, int length1, int length2) {
    for (int i = 17; i >= 10; i--) {
        if (s1[length1 - i] > s2[length2 - i]) {
            return 1;
        }
        if (s1[length1 - i] < s2[length2 - i]) {
            return 0;
        }
    }
    return 0;
}

int sortEnd(char* s1, char* s2, int length1, int length2) {
    for (int i = 8; i >= 0; i--) {
        if (s1[length1 - i] > s2[length2 - i]) {
            return 1;
        }
        if (s1[length1 - i] < s2[length2 - i]) {
            return 0;
        }
    }
    return 0;
}

void helper(PERSON* p, int length) {
    for (int i = length - 1; i >= 0; i--) {
        for (int j = 0; j < i; j++) {
            if (sortStart(p[j].str, p[j + 1].str, strlen(p[j].str),
                          strlen(p[j + 1].str))) {
                char* tmp = p[j].str;
                p[j].str = p[j + 1].str;
                p[j + 1].str = tmp;
            }
        }
    }
    //输出start时候
    char* start = p[0].str;
    int i = 0;
    while (start[i] != ' ') {
        printf("%c", start[i++]);
    }
    for (int i = length - 1; i >= 0; i--) {
        for (int j = 0; j < i; j++) {
            if (sortEnd(p[j].str, p[j + 1].str, strlen(p[j].str),
                        strlen(p[j + 1].str))) {
                char* tmp = p[j].str;
                p[j].str = p[j + 1].str;
                p[j + 1].str = tmp;
            }
        }
    }
    printf(" ");
    //输出end时候
    char* end = p[length - 1].str;
    int j = 0;
    while (end[j] != ' ') {
        printf("%c", end[j++]);
    }
    printf("\n");
}

int main() {
    int num;
    FILE* fp = fopen("./data.in", "r");
    while (fscanf(fp, "%d\n", &num) != EOF) {
        PERSON* tmp = (PERSON*)malloc(sizeof(PERSON) * num);
        unsigned long int size = 0;
        for (int i = 0; i < num; i++) {
            getline(&tmp[i].str, &size, fp);
            if (tmp[i].str[strlen(tmp[i].str) - 1] == '\n') {
                tmp[i].str[strlen(tmp[i].str) - 1] = '\0';
            }
        }
        helper(tmp, num);
    }
}